// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.pages.forum;

import java.util.List;

import lichen.entities.forum.Forum;
import lichen.entities.forum.ForumImpl;
import lichen.entities.post.Topic;
import lichen.pages.Operations;
import lichen.pages.base.BasePage;
import lichen.pages.base.LogInfoPage;
import lichen.pages.base.MenuPage;
import lichen.pages.topic.ViewTopic;
import lichen.services.Menu;
import lichen.services.UserService;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.corelib.components.Loop;
import org.apache.tapestry.corelib.components.PageLink;

/**
 * 浏览forum的页面
 * 
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 131 $
 * @since 0.0.1
 */
public class ViewForum extends BasePage implements LogInfoPage,MenuPage {
	
	//查询当前论坛的所有主题
	private final static String QUERY_FORUM_TOPIC_HQL = "from "
			+ Topic.class.getName() + " p where p."+Topic.FORUM_PRO_NAME+"=? order by p."+Topic.CREATE_DATE_PRO_NAME+" desc";
	//查询当前论坛的子论坛
	private static final String QUERY_CHILD_FORUMS_HQL = "from "+ForumImpl.class.getName()+
			" f where f."+ForumImpl.LEFT_PRO_NAME+">? and f."+ForumImpl.RIGHT_PRO_NAME+"<? and f."+ForumImpl.DEPTH_PRO_NAME+"=?";

	
	//当前的论坛对象
	private Forum forum;
	
	@Inject
	private UserService _userService;

	//对论坛主题的循环操作.
	@SuppressWarnings("unused")
	@Component(parameters = { "source=topics", "value=topic" })
	private Loop topicLoop;
	
	//对子论坛的循环操作.
	@SuppressWarnings("unused")
	@Component(parameters = { "source=childForums", "value=childForum" })
	private Loop childForumLoop;
	
	//循环中的论坛主题的临时变量
	private Topic topic;
	//循环中子论坛的临时变量
	private Forum childForum;
	private List childForums;
	
	//跳转到论坛的组件
	@SuppressWarnings("unused")
	@Component(parameters = {"page=forum/view","context=childForum"})
	private PageLink viewForum;
	//新建主题的链接
	@SuppressWarnings("unused")
	@Component(parameters = {"page=topic/form","context=newActionParameters"})
	private PageLink newTopicLink;
	//查看主题的链接
	@SuppressWarnings("unused")
	@Component(parameters = {"page=topic/view","context=viewTopicActionParameters"})
	private PageLink viewTopicLink;
	
		
	// 当页面加载时候
	void onActivate(Forum forum) {
		this.forum=(Forum) forum;
		fetchChildForums();
	}
	//固化的值
	Forum onPassivate() {
		return forum;
	}

	/**
	 * 得到当前的论坛对象
	 * @return Returns the forum.
	 */
	public Forum getForum() {
		return forum;
	}
	public void setForum(Forum forum){
		this.forum=forum;
	}

	/**
	 * 得到当前论坛的线程集合. 
	 * @return 当前论坛的线程集合.
	 */
	public List getTopics() {
		return getEntityService().find(QUERY_FORUM_TOPIC_HQL, this.forum);
	}
	/**
	 * 得到子论坛的集合.
	 * @return 得到子论坛的集合.
	 */
	public List getChildForums(){
		return this.childForums;
	}
	public boolean isHasChildForums(){
		return this.childForums.size()>0;
	}
	public boolean isHasPosts(){
		return this.forum.getDepth()>1;
	}
	
	private void fetchChildForums(){
		this.childForums = getEntityService().find(QUERY_CHILD_FORUMS_HQL,
				new Object[]{forum.getLeft(),forum.getRight(),forum.getDepth()+1});
		
	}
	/**
	 * @return Returns the childForum.
	 */
	public Forum getChildForum() {
		return childForum;
	}
	/**
	 * @param childForum The childForum to set.
	 */
	public void setChildForum(Forum childForum) {
		this.childForum = childForum;
	}
	

	/**
	 * 删除论坛的主题.
	 * @param topic 论坛主题.
	 */
	public void onActionFromDeleteTopic(Topic topic) {
		getEntityService().delete(topic);
	}
	
	/**
	 * 得到新增帖子的参数
	 * @return 新建帖子时候的参数.
	 */
	public Object[] getNewActionParameters(){
		return new Object[]{Operations.NEW,this.forum};
	}
	/**
	 * 得到编辑帖子的参数
	 * @return 编辑帖子时候的参数.
	 */
	public Object[] getEditActionParameters(){
		return new Object[]{Operations.EDIT,this.topic};
	}
	/**
	 * 查看帖子的参数
	 * @return 查看帖子时候的参数.
	 */
	public Object[] getViewTopicActionParameters(){
		return new Object[]{this.topic,ViewTopic.FLAT_VIEW};
	}
	
	/**
	 * @return Returns the topic.
	 */
	public Topic getTopic() {
		return topic;
	}
	/**
	 * @param topic The topic to set.
	 */
	public void setTopic(Topic topic) {
		this.topic = topic;
	}
	/**
	 * 
	 * @see lichen.pages.base.LogInfoPage#getLogInfo()
	 */
	public String getLogInfo() {
		return this.getMessages().format("forum.view",this.forum.getName());
	}
	public List<Menu> contributeMenus() {
		return this.getMenuService().generateForumMenu(this.forum);
	}
	
	/**
	 * 使用UserService中的isLogin方法来校验用户是否登录
	 * @return true:登录 false:没有登录
	 * @since 0.0.3
	 */
	public boolean isLogin(){
		return _userService.isSystemUser();
	}
}
